package com.warom.sdg.websocket;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * WebSocket配置类
 * 负责管理WebSocket相关配置
 */
public class WebSocketConfig {
    private static final String TAG = "WebSocketConfig";
    
    // SharedPreferences名称和键值
    private static final String PREFS_NAME = "websocket_config";
    private static final String KEY_WS_URL = "ws_url";
    private static final String KEY_HEARTBEAT_INTERVAL = "heartbeat_interval";
    private static final String KEY_AUTO_RECONNECT = "auto_reconnect";
    private static final String KEY_MAX_RECONNECT_ATTEMPTS = "max_reconnect_attempts";
    
    // 默认配置值
    private static final String DEFAULT_WS_URL = "ws://222.222.67.42:19995/ws/kinggu";
    private static final int DEFAULT_HEARTBEAT_INTERVAL = 30; // 秒
    private static final boolean DEFAULT_AUTO_RECONNECT = true;
    // 设置为Integer.MAX_VALUE表示无限重试
    private static final int DEFAULT_MAX_RECONNECT_ATTEMPTS = Integer.MAX_VALUE;
    
    // 单例实例
    private static WebSocketConfig instance;
    
    // SharedPreferences实例
    private final SharedPreferences prefs;
    
    /**
     * 获取WebSocketConfig单例实例
     * @param context 上下文
     * @return WebSocketConfig实例
     */
    public static synchronized WebSocketConfig getInstance(Context context) {
        if (instance == null) {
            instance = new WebSocketConfig(context.getApplicationContext());
        }
        return instance;
    }
    
    /**
     * 私有构造函数
     * @param context 上下文
     */
    private WebSocketConfig(Context context) {
        prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }
    
    /**
     * 获取WebSocket服务器URL
     * @return WebSocket URL
     */
    public String getWebSocketUrl() {
        return prefs.getString(KEY_WS_URL, DEFAULT_WS_URL);
    }
    
    /**
     * 设置WebSocket服务器URL
     * @param url WebSocket URL
     */
    public void setWebSocketUrl(String url) {
        prefs.edit().putString(KEY_WS_URL, url).apply();
        Log.d(TAG, "WebSocket URL已更新: " + url);
    }
    
    /**
     * 获取心跳间隔（秒）
     * @return 心跳间隔
     */
    public int getHeartbeatInterval() {
        return prefs.getInt(KEY_HEARTBEAT_INTERVAL, DEFAULT_HEARTBEAT_INTERVAL);
    }
    
    /**
     * 设置心跳间隔（秒）
     * @param interval 心跳间隔
     */
    public void setHeartbeatInterval(int interval) {
        if (interval < 5) {
            interval = 5; // 最小心跳间隔为5秒
        }
        prefs.edit().putInt(KEY_HEARTBEAT_INTERVAL, interval).apply();
    }
    
    /**
     * 是否自动重连
     * @return 是否自动重连
     */
    public boolean isAutoReconnect() {
        return prefs.getBoolean(KEY_AUTO_RECONNECT, DEFAULT_AUTO_RECONNECT);
    }
    
    /**
     * 设置是否自动重连
     * @param autoReconnect 是否自动重连
     */
    public void setAutoReconnect(boolean autoReconnect) {
        prefs.edit().putBoolean(KEY_AUTO_RECONNECT, autoReconnect).apply();
    }
    
    /**
     * 获取最大重连尝试次数
     * @return 最大重连尝试次数
     */
    public int getMaxReconnectAttempts() {
        return prefs.getInt(KEY_MAX_RECONNECT_ATTEMPTS, DEFAULT_MAX_RECONNECT_ATTEMPTS);
    }
    
    /**
     * 设置最大重连尝试次数
     * @param attempts 最大重连尝试次数
     */
    public void setMaxReconnectAttempts(int attempts) {
        if (attempts < 1) {
            attempts = 1; // 最小尝试次数为1
        }
        prefs.edit().putInt(KEY_MAX_RECONNECT_ATTEMPTS, attempts).apply();
    }
    
    /**
     * 重置所有配置为默认值
     */
    public void resetToDefaults() {
        prefs.edit()
                .putString(KEY_WS_URL, DEFAULT_WS_URL)
                .putInt(KEY_HEARTBEAT_INTERVAL, DEFAULT_HEARTBEAT_INTERVAL)
                .putBoolean(KEY_AUTO_RECONNECT, DEFAULT_AUTO_RECONNECT)
                .putInt(KEY_MAX_RECONNECT_ATTEMPTS, DEFAULT_MAX_RECONNECT_ATTEMPTS)
                .apply();
        
        Log.d(TAG, "WebSocket配置已重置为默认值");
    }
} 